// services/domains.ts - 虚拟机管理 API 服务
import apiClient from './api';
import type {
  DomainDetail,
  DomainSummary,
  CreateDomainRequest,
  ListResponse,
  ApiResponse,
  DomainStats,
} from '../types/api';

/**
 * 获取虚拟机列表
 * @param connection 连接ID
 * @param includeInactive 是否包含未激活的虚拟机
 */
export const listDomains = async (
  connection: string,
  includeInactive: boolean = true
): Promise<ListResponse<DomainSummary>> => {
  const response = await apiClient.get<ListResponse<DomainSummary>>('/domains', {
    params: { connection, inactive: includeInactive },
  });
  return response.data;
};

/**
 * 获取虚拟机详细信息
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const getDomainDetail = async (
  connection: string,
  name: string
): Promise<DomainDetail> => {
  const response = await apiClient.get<DomainDetail>('/domain', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 创建虚拟机
 * @param connection 连接ID
 * @param domainConfig 虚拟机配置
 */
export const createDomain = async (
  connection: string,
  domainConfig: CreateDomainRequest
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domains', domainConfig, {
    params: { connection },
  });
  return response.data;
};

/**
 * 启动虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const startDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/start', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 关闭虚拟机（优雅关机）
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const shutdownDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/shutdown', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 强制停止虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const forceStopDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/force-stop', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 重启虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const rebootDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/reboot', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 暂停虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const suspendDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/suspend', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 恢复虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const resumeDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/domain/resume', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 删除虚拟机
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const deleteDomain = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.delete<ApiResponse>('/domain', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 获取虚拟机状态
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const getDomainStatus = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.get<ApiResponse>('/domain/status', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 获取虚拟机统计信息
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const getDomainStats = async (
  connection: string,
  name: string
): Promise<DomainStats> => {
  const response = await apiClient.get<DomainStats>('/domain/stats', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 获取虚拟机 XML 配置
 * @param connection 连接ID
 * @param name 虚拟机名称
 */
export const getDomainXML = async (
  connection: string,
  name: string
): Promise<string> => {
  const response = await apiClient.get<string>('/domain/xml', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * 设置虚拟机内存
 * @param connection 连接ID
 * @param name 虚拟机名称
 * @param memory 内存大小（KiB）
 */
export const setDomainMemory = async (
  connection: string,
  name: string,
  memory: number
): Promise<ApiResponse> => {
  const response = await apiClient.put<ApiResponse>(
    '/domain/memory',
    { memory },
    { params: { connection, name } }
  );
  return response.data;
};

/**
 * 设置虚拟机 vCPU 数量
 * @param connection 连接ID
 * @param name 虚拟机名称
 * @param vcpus vCPU 数量
 */
export const setDomainVCPUs = async (
  connection: string,
  name: string,
  vcpus: number
): Promise<ApiResponse> => {
  const response = await apiClient.put<ApiResponse>(
    '/domain/vcpus',
    { vcpus },
    { params: { connection, name } }
  );
  return response.data;
};

/**
 * 生成虚拟机 XML 模板
 * @param domainConfig 虚拟机配置
 */
export const generateDomainTemplate = async (
  domainConfig: CreateDomainRequest
): Promise<string> => {
  const response = await apiClient.post<string>('/templates/generate', domainConfig);
  return response.data;
};

// 导出所有服务
export const domainService = {
  listDomains,
  getDomainDetail,
  createDomain,
  startDomain,
  shutdownDomain,
  forceStopDomain,
  rebootDomain,
  suspendDomain,
  resumeDomain,
  deleteDomain,
  getDomainStatus,
  getDomainStats,
  getDomainXML,
  setDomainMemory,
  setDomainVCPUs,
  generateDomainTemplate,
};